home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MatteBorder.java < prev    next >
Text File  |  1998-06-30  |  7KB  |  186 lines

  1. /*
  2.  * @(#)MatteBorder.java    1.10 98/02/24
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20. package com.sun.java.swing.border;
  21.  
  22. import java.awt.Graphics;
  23. import java.awt.Insets;
  24. import java.awt.Rectangle;
  25. import java.awt.Component;
  26. import java.awt.Color;
  27.  
  28. import com.sun.java.swing.Icon;
  29.  
  30. /**
  31.  * A class which provides a matte-like border of either a solid color 
  32.  * or a tiled icon.
  33.  * <p>
  34.  * Warning: serialized objects of this class will not be compatible with
  35.  * future swing releases.  The current serialization support is appropriate
  36.  * for short term storage or RMI between Swing1.0 applications.  It will
  37.  * not be possible to load serialized Swing1.0 objects with future releases
  38.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  39.  * baseline for the serialized form of Swing objects.
  40.  *
  41.  * @version 1.10 02/24/98
  42.  * @author Amy Fowler
  43.  */
  44. public class MatteBorder extends EmptyBorder
  45. {
  46.     protected Color color;
  47.     protected Icon tileIcon;
  48.  
  49.     /**
  50.      * Creates a matte border with the specified insets and color.
  51.      * @param top the top inset of the border
  52.      * @param left the left inset of the border
  53.      * @param bottom the bottom inset of the border
  54.      * @param right the right inset of the border
  55.      */
  56.     public MatteBorder(int top, int left, int bottom, int right, Color color)   {
  57.         super(top, left, bottom, right);
  58.         this.color = color;
  59.     }
  60.  
  61.     /**
  62.      * Creates a matte border with the specified insets and tile icon.
  63.      * @param top the top inset of the border
  64.      * @param left the left inset of the border
  65.      * @param bottom the bottom inset of the border
  66.      * @param right the right inset of the border
  67.      * @param tileIcon the icon to be used for tiling the border
  68.      */
  69.     public MatteBorder(int top, int left, int bottom, int right, Icon tileIcon)   {
  70.         super(top, left, bottom, right);
  71.         this.tileIcon = tileIcon;
  72.     }
  73.  
  74.     /**
  75.      * Creates a matte border with the specified tile icon.  The
  76.      * insets will be calculated dynamically based on the size of
  77.      * the tile icon, where the top and bottom will be equal to the
  78.      * tile icon's height, and the left and right will be equal to
  79.      * the tile icon's width.
  80.      * @param tileIcon the icon to be used for tiling the border
  81.      */
  82.     public MatteBorder(Icon tileIcon)   {
  83.         this(-1,-1,-1,-1, tileIcon);
  84.     }
  85.  
  86.     /**
  87.      * Paints the matte border.
  88.      */
  89.     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  90.         Insets insets = getBorderInsets(c);
  91.         Color oldColor = g.getColor();
  92.         g.translate(x, y);
  93.  
  94.         // If the tileIcon failed loading, paint as gray.
  95.         if (tileIcon != null) {
  96.             color = (tileIcon.getIconWidth() == -1) ? Color.gray : null;
  97.         }
  98.  
  99.         if (color != null) {
  100.             g.setColor(color);
  101.             g.fillRect(0, 0, width - insets.right, insets.top);
  102.             g.fillRect(0, insets.top, insets.left, height - insets.top);
  103.             g.fillRect(insets.left, height - insets.bottom, width - insets.left, insets.bottom);
  104.             g.fillRect(width - insets.right, 0, insets.right, height - insets.bottom);
  105.  
  106.         } else if (tileIcon != null) {
  107.  
  108.             int tileW = tileIcon.getIconWidth();
  109.             int tileH = tileIcon.getIconHeight();
  110.             int xpos, ypos, startx, starty;
  111.             Graphics cg;
  112.  
  113.             // Paint top matte edge
  114.             cg = g.create();
  115.             cg.setClip(0, 0, width, insets.top);
  116.             for (ypos = 0; insets.top - ypos > 0; ypos += tileH) {
  117.                 for (xpos = 0; width - xpos > 0; xpos += tileW) {
  118.                     tileIcon.paintIcon(c, cg, xpos, ypos);
  119.                 }
  120.             }
  121.             cg.dispose();
  122.  
  123.             // Paint left matte edge
  124.             cg = g.create();
  125.             cg.setClip(0, insets.top, insets.left, height - insets.top);
  126.             starty = insets.top - (insets.top%tileH);
  127.             startx = 0;
  128.             for (ypos = starty; height - ypos > 0; ypos += tileH) {
  129.                 for (xpos = startx; insets.left - xpos > 0; xpos += tileW) {
  130.                     tileIcon.paintIcon(c, cg, xpos, ypos);
  131.                 }
  132.             }
  133.             cg.dispose();
  134.  
  135.             // Paint bottom matte edge
  136.             cg = g.create();
  137.             cg.setClip(insets.left, height - insets.bottom, width - insets.left, insets.bottom);
  138.             starty = (height - insets.bottom) - ((height - insets.bottom)%tileH);
  139.             startx = insets.left - (insets.left%tileW);
  140.             for (ypos = starty; height - ypos > 0; ypos += tileH) {
  141.                 for (xpos = startx; width - xpos > 0; xpos += tileW) {
  142.                     tileIcon.paintIcon(c, cg, xpos, ypos);
  143.                 }
  144.             }
  145.             cg.dispose();
  146.  
  147.             // Paint right matte edge
  148.             cg = g.create();
  149.             cg.setClip(width - insets.right, insets.top, insets.right, height - insets.top - insets.bottom);
  150.             starty = insets.top - (insets.top%tileH);
  151.             startx = width - insets.right - ((width - insets.right)%tileW);
  152.             for (ypos = starty; height - ypos > 0; ypos += tileH) {
  153.                 for (xpos = startx; width - xpos > 0; xpos += tileW) {
  154.                     tileIcon.paintIcon(c, cg, xpos, ypos);
  155.                 }
  156.             }
  157.             cg.dispose();
  158.         }
  159.         g.translate(-x, -y);
  160.         g.setColor(oldColor);
  161.  
  162.     }
  163.  
  164.     /**
  165.      * Returns the insets of the border.
  166.      * @param c the component for which this border insets value applies
  167.      */
  168.     public Insets getBorderInsets(Component c) {
  169.         if (tileIcon != null && top == -1 && bottom == -1 && left == -1 && right == -1) {
  170.             int w = tileIcon.getIconWidth();
  171.             int h = tileIcon.getIconHeight();
  172.             return new Insets(h,w,h,w);
  173.         }
  174.         return new Insets(top, left, bottom, right);
  175.     }
  176.  
  177.     /**
  178.      * Returns whether or not the border is opaque.
  179.      */
  180.     public boolean isBorderOpaque() { 
  181.         // If a tileIcon is set, then it may contain transparent bits
  182.         return color != null; 
  183.     }
  184.  
  185. }
  186.